local localPlayer = getLocalPlayer()
local playerName = getPlayerName(localPlayer)

local x, y = guiGetScreenSize()
local x, y = x / 2, y / 2
local handlers = 0


function createPasswordWindow()
	windowChangepw = guiCreateWindow(0.3859,0.349,0.2219,0.1628,"[P-Login] - Change password",true)
	guiWindowSetMovable ( windowChangepw, false )
	guiWindowSetSizable ( windowChangepw, false )
	guiSetSize(windowChangepw, 165, 21)
	guiSetAlpha(windowChangepw,0.80000001192093)
	editOldpw = guiCreateEdit(110,29,165,21,"",false,windowChangepw)
	guiSetAlpha(editOldpw,1)
	guiEditSetMasked(editOldpw,true)
	labelOldpw = guiCreateLabel(10,29,90,21,"Old password:",false,windowChangepw)
	guiSetAlpha(labelOldpw,1)
	guiLabelSetColor(labelOldpw,255,255,255)
	guiLabelSetVerticalAlign(labelOldpw,"center")
	guiLabelSetHorizontalAlign(labelOldpw,"left",false)
	editNewpw = guiCreateEdit(110,60,165,21,"",false,windowChangepw)
	guiSetAlpha(editNewpw,1)
	guiEditSetMasked(editNewpw,true)
	guiEditSetMaxLength(editNewpw,50)
	labelNewpw = guiCreateLabel(10,60,90,21,"New password:",false,windowChangepw)
	guiSetAlpha(labelNewpw,1)
	guiLabelSetColor(labelNewpw,255,255,255)
	guiLabelSetVerticalAlign(labelNewpw,"center")
	guiLabelSetHorizontalAlign(labelNewpw,"left",false)
	buttonChangepw = guiCreateButton(10,91,265,23,"Change password",false,windowChangepw)
	guiSetAlpha(buttonChangepw,1)
	
	guiSetVisible(windowChangepw, false)
	
	addEventHandler("onClientGUIClick", buttonChangepw, clientSubmitChangepw, false)
end

function createLoginWindow()
	windowLogin = guiCreateWindow((x-150),(y-125),270,165,"All Points Bulletin",false)
	guiWindowSetMovable ( windowLogin, false )
	guiWindowSetSizable ( windowLogin, false )
	guiSetAlpha(windowLogin,1)
	labelUsername = guiCreateLabel(10,62,59,24,"Username:",false,windowLogin)
	guiSetAlpha(labelUsername,1)
	guiLabelSetColor(labelUsername,255,255,255)
	guiLabelSetVerticalAlign(labelUsername,"center")
	guiLabelSetHorizontalAlign(labelUsername,"left",false)
	labelPassword = guiCreateLabel(10,96,59,24,"Password:",false,windowLogin)
	guiSetAlpha(labelPassword,1)
	guiLabelSetColor(labelPassword,255,255,255)
	guiLabelSetVerticalAlign(labelPassword,"center")
	guiLabelSetHorizontalAlign(labelPassword,"left",false)
	labelInfo = guiCreateLabel(10,26,250,27,"Please login or register.",false,windowLogin)
	guiSetAlpha(labelInfo,1)
	guiLabelSetColor(labelInfo,255,255,255)
	guiLabelSetVerticalAlign(labelInfo,"top")
	guiLabelSetHorizontalAlign(labelInfo,"center",true)
	guiSetFont(labelInfo,"default-bold-small")
	editUsername = guiCreateEdit(79,62,181,25,playerName,false,windowLogin)
	guiSetAlpha(editUsername,1)
	guiEditSetMaxLength(editUsername, 50)
	editPassword = guiCreateEdit(79,96,181,25,"",false,windowLogin)
	guiSetAlpha(editPassword,1)
	guiEditSetMasked(editPassword, true)
	guiEditSetMaxLength(editPassword, 50)
	buttonLogin = guiCreateButton(10,131,120,21,"Log in",false,windowLogin)
	guiSetAlpha(buttonLogin,1)
	buttonRegister = guiCreateButton(143,131,117,21,"Register",false,windowLogin)
	guiSetAlpha(buttonRegister,1)
	
	guiSetVisible(windowLogin, false)
	
	addEventHandler("onClientGUIClick", buttonLogin, clientSubmitLogin, false)
	addEventHandler("onClientGUIClick", buttonRegister, clientSubmitRegister, false)
end

function resourceStart()
	createLoginWindow()
	triggerServerEvent ( "clientReady", getLocalPlayer() )
end

function changePw()
	createPasswordWindow()
	guiSetVisible(windowChangepw, true)
	showCursor(true)
	guiSetInputEnabled(true)
end

function clientSubmitLogin(button, state)
	if button == "left" and state == "up" then
		local username = guiGetText(editUsername)
		local password = guiGetText(editPassword)
		if username and password then
			triggerServerEvent("submitLogin", localPlayer, localPlayer, username, password)
		else
			guiSetText(labelInfo, "Enter username and password.")
		end
	end
end

function clientSubmitRegister(button, state)
	if button == "left" and state == "up" then
		local username = guiGetText(editUsername)
		local password = guiGetText(editPassword)
		if username and password then
			triggerServerEvent("submitRegister", localPlayer, localPlayer, username, password)
		else
			guiSetText(labelInfo, "Enter username and password.")
		end
	end
end

function clientSubmitChangepw(button, state)
	if button == "left" and state == "up" then
		local oldpassword = guiGetText(editOldpw)
		local newpassword = guiGetText(editNewpw)
		if oldpassword and newpassword then
			triggerServerEvent("submitChangepw", localPlayer, localPlayer, oldpassword, newpassword)
		else
			outputChatBox("Enter old and new password.")
		end
	end
end

function accountKnown()
	triggerEvent ("hideLoginWindow", localPlayer)
	triggerServerEvent ("loggedIn", localPlayer)
end

function showLoginWindow()
	removeEventHandler ( "onClientGUIClick", buttonLogin, accountKnown )
	removeEventHandler ( "onClientGUIClick", buttonRegister, logOutOnClick )
	removeEventHandler ( "onClientGUIClick", buttonLogin, clientSubmitLogin )
	removeEventHandler ( "onClientGUIClick", buttonRegister, clientSubmitRegister )
	addEventHandler("onClientGUIClick", buttonLogin, clientSubmitLogin, false)
	addEventHandler("onClientGUIClick", buttonRegister, clientSubmitRegister, false)
	guiSetText(buttonLogin, "Login")
	guiSetText(buttonRegister, "Register")
	guiSetText(labelInfo, "Please login or register.")
	handlers = 0
	guiSetInputEnabled(true)
	guiSetVisible(windowLogin, true)
	showCursor(true)
end

function hideLoginWindow()
	removeEventHandler ( "onClientGUIClick", buttonLogin, accountKnown )
	removeEventHandler ( "onClientGUIClick", buttonRegister, logOutOnClick )
	removeEventHandler ( "onClientGUIClick", buttonLogin, clientSubmitLogin )
	removeEventHandler ( "onClientGUIClick", buttonRegister, clientSubmitRegister )
	addEventHandler("onClientGUIClick", buttonLogin, clientSubmitLogin, false)
	addEventHandler("onClientGUIClick", buttonRegister, clientSubmitRegister, false)
	guiSetText(buttonLogin, "Login")
	guiSetText(buttonRegister, "Register")
	guiSetText(labelInfo, "Please login or register.")
	handlers = 0
	guiSetVisible(windowLogin, false)
end

function hidePasswordWindow()
	guiSetInputEnabled(false)
	guiSetVisible(windowChangepw, false)
	showCursor(false)
end

function unknownError()
	guiSetText(labelInfo, "An unknown error occured.")
end

function loginWrong()
	guiSetText(labelInfo, "Wrong username and/or password.")
end

function registerTaken()
	guiSetText(labelInfo, "This username is already taken.")
end

function logOutOnClick()
	guiSetText(labelInfo, "Logged out. You may now log in.")
	triggerServerEvent("logOutPlayer", localPlayer)
	guiSetText(buttonLogin, "Login")
	guiSetText(buttonRegister, "Register")
	setTimer(changeHandlers,50,1)
end

function changeHandlers()
	if handlers == 1 then
		removeEventHandler ( "onClientGUIClick", buttonLogin, accountKnown )
		removeEventHandler ( "onClientGUIClick", buttonRegister, logOutOnClick )
		addEventHandler("onClientGUIClick", buttonLogin, clientSubmitLogin, false)
		addEventHandler("onClientGUIClick", buttonRegister, clientSubmitRegister, false)
		handlers = 0
	else
		removeEventHandler ( "onClientGUIClick", buttonLogin, clientSubmitLogin )
		removeEventHandler ( "onClientGUIClick", buttonRegister, clientSubmitRegister )
		addEventHandler("onClientGUIClick", buttonLogin, accountKnown, false)
		addEventHandler("onClientGUIClick", buttonRegister, logOutOnClick, false)
		handlers = 1
	end
end

function alreadyLogged()
	guiSetText(labelInfo, "You are already logged in. Would you like to use this account ?")
	guiSetText(buttonLogin, "Yes")
	guiSetText(buttonRegister, "No")
	setTimer(changeHandlers,50,1)
end

addEvent("alreadyLogged", true)
addEvent("hidePasswordWindow", true)
addEvent("hideLoginWindow", true)
addEvent("unknownError", true)
addEvent("loginWrong", true)
addEvent("registerTaken", true)
addEvent("showLoginWindow", true)
addEventHandler("alreadyLogged", localPlayer, alreadyLogged)
addEventHandler("showLoginWindow", localPlayer, showLoginWindow)
addEventHandler("hidePasswordWindow", localPlayer, hidePasswordWindow)
addEventHandler("hideLoginWindow", localPlayer, hideLoginWindow)
addEventHandler("unknownError", localPlayer, unknownError)
addEventHandler("loginWrong", localPlayer, loginWrong)
addEventHandler("registerTaken", localPlayer, registerTaken)
addCommandHandler("changepw", changePw)
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), resourceStart)